<?php 

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$page_title = "Product Page";
require_once ('./includes/sidebar.php');  
echo '<link href="./includes/style.css" rel="stylesheet" type="text/css" />';     



// Check for a general product ID in the URL.
$name = NULL;
if(isset($_GET['gw_id'])) 
{

	// Typecast it to an integer:
	$gw_id = (int) $_GET['gw_id'];	
	// $gw_id must have a valid value.
    if ($gw_id > 0) 
    {  
            $gw_name = $_GET['gw_name'];
            $cid = $_GET['cid'];
            $category = $_GET['category'];
            

            $parent_category_id = $_GET['parent_category_id'];
            $parent_category = $_GET['parent_category_name'];
            if(isset($_GET['page']))
            {
                $page = $_GET['page'];
            }
            else
            {
                $page = 1;
            }
            
            echo '<br />  <div class="menu2">'; 
            echo ' <b><a href="category.php?page=' . $page . '&category=' . $category . '&cid=' . $cid . '&parent_category_id=' . $parent_category_id . '&parent_category_name=' . $parent_category . '">' . $category . '</a></b>';                                                                                                                                                                                   
	        echo '</div>';
            echo '<div class="subcategoryheader">' . $gw_name . '</div>';    
		    
            
    ?>
            
            


    <?php

            $getRequest = new item();
            $getRequest->gw_id = $gw_id;
            
            $getResponse = $getRequest->getitembyid(); 
            $item_xml = new SimpleXMLElement($getResponse);
             foreach ($item_xml->Item as $ItemXml)
             {                           
                     $item_gw_id = $ItemXml->gw_id;
                     $item_gw_name = $ItemXml->gw_name;
                     $item_category_id = $ItemXml->category_id;
                     $item_general_description = $ItemXml->general_description;
                     $item_description = (string)$ItemXml->description;
                     $item_description = urldecode($item_description);
                     $item_image = (string)$ItemXml->image;
                     $item_image = urldecode($item_image);
                     $item_price = $ItemXml->price;
                     $item_in_stock = $ItemXml->in_stock;
             }

            

            $getrequest = new itemspecialization();
            $getrequest->gw_id = $gw_id;
            $getresponse = $getrequest->getspecificwidgetbygwid();
            $xml = new SimpleXMLElement($getresponse);
            $specialized_state = $xml->Item_Count;
            $specialized_state = (int)$specialized_state;
           
            
            $total_items = 0; 
            $num_valid_items = 0;     
            if($specialized_state != 0)
            {
                $specialize = "true";


                    ?>
            <script>

    function swapimage()
    {
                var Images = new Array();
    <?php
                 
                $specialization_description_array = array();    
                $first_image = $item_image; 
                $temp_int = 0;  
                $weight_array = array();
                $price_array = array();
                $quantity_array = array();
                foreach($xml->Item as $eachitem2)
                {   
                    $descriptor_build = "";
                    $sw_id = $eachitem2->sw_id;
                    $sw_id = (string) $sw_id; 
                    $quantity = $eachitem2->qty;
                    $quantity = (int)$quantity;
                    $price = $eachitem2->price; 
                    $price = (int)$price;
                    $weight = (int)$eachitem2->weight;
                    $weight_array[$sw_id] = $weight;
                    $price_array[$sw_id] = $price;
                    $quantity_array[$sw_id] = $quantity;                          
                    $image = (string)$eachitem2->image;
                    $image = urldecode($image);
                    if($temp_int == 0 && $price > 0 && $weight > 0 && $quantity > 0)
                    {
                        $first_image = $image;
                        $temp_int++;
                    }
                    $image = checkimage($image);                       
                    echo 'Images["' . $sw_id . '"] = "' . $image . '";';
                    $getrequest3 = new itemspecialization();
                    $getrequest3->sw_id = $sw_id;
                    $getresponse3 = $getrequest3->getisdidbyspecificwidgetid();
                    $xml3 = new SimpleXMLElement($getresponse3);
                    $isd_id_array = array();
                    $numm = 0;
                    $descriptor_array = array();                       
                    foreach($xml3->Item as $eachitem3)
                    {
                        $isd_id = $eachitem3->isd_id;  
                        $isd_id = (string)$isd_id;    
                        $getrequest4 = new itemspecialization();
                        $getrequest4->isd_id = $isd_id;
                        $getresponse4 = $getrequest4->getitemspecializationdescriptorbyisdid();
                        $xml4 = new SimpleXMLElement($getresponse4);
                        foreach($xml4->Item as $eachitem4)
                        {
                            $descriptor = $eachitem4->descriptor;
                            $descriptor = (string)$descriptor;
                            $isc_id2 = $eachitem4->isc_id;
                            $isc_id2 = (string)$isc_id2;
                            $getrequest5 = new itemspecialization();
                            $getrequest5->isc_id = $isc_id2;                    
                            $getresponse5 = $getrequest5->getitemspecializationcategorybyiscid();
                            $xml5 = new SimpleXMLElement($getresponse5);
                            foreach($xml5->Item as $eachitem5)
                            {
                                $is_category = $eachitem5->category;
                                $is_category = (string)$is_category;                                  
                            }  
                            $descriptor_array[$is_category] = $descriptor;
                            $isd_id_array[$descriptor] = $isd_id;                                  
                        }   
                        $numm++; 
                    }
                    ksort($descriptor_array);                     
                    foreach($descriptor_array as $key => $value)
                    {
                        $category = $key;
                        $descriptor = $value;
                        $descriptor_build .= $category . "=" . $descriptor . ", ";
                    }                       
                    $descriptor_build .= " Price=$" . $price; 
                    $specialization_description_array[$sw_id] = $descriptor_build;       
                    $total_items++;      
                }             
    ?>

       var image = document.getElementById("imageToSwap"); 
       var changed_sw_id = document.getElementById("sw_id");
       var imagesrc = Images[changed_sw_id.value];
       image.src = imagesrc;
        
    }
        </script>
    <?php    
           
            if($total_items != 0)
            { 
                
    ?> 
                <br /><br />
                <form action="product.php">
                    <tr>
                        <td align="center">
                        <?php echo imageresizedimensions("imageToSwap", .75, $first_image);?>
                        </td>
                    </tr><br />
                    <tr>
                        <td align="left"><b>Name:</b><?php echo $item_gw_name?></td>
                    </tr>
                    <br />
                    <select id="sw_id" name="sw_id" onchange="swapimage()">
    <?php
                                
                    $counter = 0;
                    foreach($specialization_description_array as $key => $value)
                    {                               
                        $current_sw_id = $key;
                        $current_weight = $weight_array[$current_sw_id];
                        $current_quantity = $quantity_array[$current_sw_id];
                        $current_price = $price_array[$current_sw_id];
                        if($current_weight > 0 && $current_quantity > 0 && $current_price > 0)
                        {
                            $current_description_build = $value;
                            echo '<option value="' . $current_sw_id . '">' . $current_description_build . '</option>';
                            $counter++;
                        }
                                    
                                   
                    }
                    if($counter == 0)
                    {
                        echo '<option value="0">Out Of Order</option';
                    }            
    ?>
                    </select>                 
                    <br />
<?php
                     if($counter > 0)
                     {
                         echo '<br /><input type="submit" id="cart_button" name="cart_button" value="Add To Cart"><br /><br />     ';
                     }
?>
                     <tr>
                        <td align="left"><b>Product Information:</b><?php echo $item_description; ?></a></td>
                     </tr> 
                     <br />
   
                   
                     <br />               
                    
                     <input type="hidden" id="do" name="do" value="add">                        
                     <input type="hidden" id="page_title" name="page_title" value="<?php echo $_GET['page_title']; ?>">
                     <input type="hidden" id="gw_name" name="gw_name" value="<?php echo $_GET['gw_name']; ?>">        
                     <input type="hidden" id="cid" name="cid" value="<?php echo $_GET['cid']; ?>">        
                     <input type="hidden" id="category" name="category" value="<?php echo $_GET['category']; ?>">        
                     <input type="hidden" id="parent_category_id" name="parent_category_id" value="<?php echo $_GET['parent_category_id']; ?>">        
                     <input type="hidden" id="parent_category" name="parent_category" value="<?php echo $_GET['parent_category']; ?>">                                      
                     <input type="hidden" id="gw_id" name="gw_id" value="<?php echo $gw_id; ?>">      
                     <input type="hidden" id="convert" name="convert" value="convert">
                     <input type="hidden" id="product" name="product" value="product">
                     <input type="hidden" id="specialized" name="specialized" value="specialized">                          
                     <input type="Hidden"name="delay">
                    
                     
                     <br />                                     
                     </form>                       
        <?php
            }  
        }    
        else
        {
                           
    ?>            <table>

                  <tr align="center">
                    <td align="center">
                        <?php echo imageresize(.75, $item_image);?>   
                    </td><br />
                  </tr><br />                  
                  <tr>  
                    <td align="left"><b>Name:</b><?php echo $item_gw_name?></td>
                  </tr><br />
                  <tr>
                        <td align="left"><b>Price:</b><?php echo $item_price; ?></td>
                   </tr><br />
                  
                   <tr>
                        <td><b><a href="product.php?page_title=<?php echo $_GET['page_title']?>&page=<?php echo $_GET['page']?>&gw_id=<?php echo $gw_id?>&gw_name=<?php echo $_GET['gw_name']?>&category=<?php echo $_GET['category']?>&parent_category_name=<?php echo $_GET['parent_category_name']?>&sw_id=&parent_category_id=<?php echo $_GET['parent_category_id'];?>&do=add&cid=<?php echo $item_category_id?>">Add To Cart</a></b></td>
                   </tr><br />
                    <tr>
                        <td align="left"><b>Product Information:</b><?php echo urldecode($item_description); ?></a></td>
                   </tr> <br />
    <?php          
            }           
    }
    echo ' </table>';     	
} // End of ($gw_id > 0) IF.
else 
{ // Invalid $_GET['gw_id']!
	echo '<p class="error">This page has been accessed in error.</p>';
}

// Include the footer file to complete the template:
include_once ('./includes/footer.html');

?>
